﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using LoanProject.Models;

namespace LoanProject.Controllers
{
    public class HomeController : Controller
    {
        private Loan mainLoan;
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            return View();
        }

        [HttpPost]
        public ActionResult Index(decimal principle, decimal installment, string startDate)
        {
            var startDate2 = (string.IsNullOrEmpty(startDate.Replace("\"", "")) ? DateTime.Now.ToString() : startDate.Replace("\"", ""));
            mainLoan = new LoanProject.Models.Loan { Principle = principle, Installment = installment , StartDate = Convert.ToDateTime(startDate2)};
            mainLoan.Cal();
            mainLoan.SaveLoan();
            var k = mainLoan.LoanMonthlys;
            return View(k);
        }

        [HttpPost]
        public ActionResult EditInstallment(int period, decimal installment)
        {
            mainLoan.LoanMonthlys.First(l => l.Period == period).Installment.Value = installment;
            mainLoan.ReCalculate(period);
            return this.Json(JsonConvert.SerializeObject(mainLoan.LoanMonthlys));
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";
            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
